package com.coder4j.starter.tools.vo;

import lombok.Data;

import java.util.Date;

/**
 * 统一应答格式
 *
 * @author Keller
 * @date 2020-12-24 10:15:31
 */
@Data
public class R<T> {
    public static final String SUCCESS_CODE = "0000";
    public static final String SUCCESS_MESSAGE = "操作成功";
    public static final String NO_DATA_CODE = "10000";
    public static final String NO_DATA_MESSAGE = "没有查询到相应数据";
    public static final String NO_ID_CODE = "20000";
    public static final String NO_ID_MESSAGE = "操作对象缺少ID字段";
    public static final String PARAM_ERROR_CODE = "30000";
    public static final String PARAM_ERROR_MESSAGE = "请求参数错误";
    public static final String ERROR_CODE = "40000";
    public static final String ERROR_MESSAGE = "服务出了点问题，请稍后";
    public static final String AUTH_FAILED_CODE = "50000";
    public static final String AUTH_FAILED_MESSAGE = "身份验证失败";
    public static final String NO_AUTH_CODE = "60000";
    public static final String NO_AUTH_MESSAGE = "无权限";

    /**
     * 业务代码
     */
    private String code;
    /**
     * 业务说明
     */
    private String message;
    /**
     * 业务数据
     */
    private T data;

    /**
     * 时间戳
     */
    private Date timeStamp = new Date();

    public R() {

    }

    private R(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private R(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public boolean isSuccess() {
        return SUCCESS_CODE.equals(code);
    }

    public static R success() {
        return new R(SUCCESS_CODE, SUCCESS_MESSAGE);
    }

    public static R success(Object data) {
        return new R(SUCCESS_CODE, SUCCESS_MESSAGE, data);
    }

    public static R error() {
        return new R(ERROR_CODE, ERROR_MESSAGE);
    }

    public static R error(String message) {
        return new R(ERROR_CODE, message);
    }

    public static R noData() {
        return new R(NO_DATA_CODE, NO_DATA_MESSAGE);
    }

    public static R noId() {
        return new R(NO_ID_CODE, NO_ID_MESSAGE);
    }

    public static R paramError() {
        return new R(PARAM_ERROR_CODE, PARAM_ERROR_MESSAGE);
    }

    public static R authFailed() {
        return new R(AUTH_FAILED_CODE, AUTH_FAILED_MESSAGE);
    }

    public static R noAuth() {
        return new R(NO_AUTH_CODE, NO_AUTH_MESSAGE);
    }
}
